//
//  ViewController.m
//  footprint
//
//  Created by SongYakun on 15/12/25.
//  Copyright © 2015年 SongYakun. All rights reserved.
//

#import "MapViewController.h"
#import "AddViewController.h"

@interface MapViewController () <MAMapViewDelegate>

@property (nonatomic, strong) MAMapView * mapView;
@property (nonatomic, strong) SkyButton * bottomButton;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeMapView];
    [self makeButton];
}

- (void)makeMapView {
    _mapView = [[MAMapView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.mapView.delegate = self;
    //取消指南针
    _mapView.showsCompass= NO;
    //打开定位
    _mapView.showsUserLocation = YES;
    //后台定位
    _mapView.pausesLocationUpdatesAutomatically = NO;
    _mapView.allowsBackgroundLocationUpdates = YES;//iOS9以上系统必须配置
    _mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    
    [_mapView setZoomLevel:16.1 animated:YES];
    [self.view addSubview:self.mapView];
}

- (void)makeButton {
    _bottomButton = [SkyButton buttonWithType:UIButtonTypeCustom];
    [self.bottomButton setFrame:CGRectZero Title:nil BackgroundColor:[UIColor grayColor] Image:nil andSelectedImage:nil];
    [self.bottomButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.mapView addSubview:self.bottomButton];
    
    [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}

- (void)buttonClicked:(UIButton *)button {
    AddViewController * addVC = [[AddViewController alloc] init];
    [self presentViewController:addVC animated:YES completion:^{
        
    }];
}
#pragma mark - MAMapViewDelegate
//当位置更新时，会进定位回调，通过回调函数，能获取到定位点的经纬度坐标
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    if(updatingLocation)
    {
        //取出当前位置的坐标
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}

- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    MAAnnotationView *view = views[0];
    
    // 放到该方法中用以保证userlocation的annotationView已经添加到地图上了。
    if ([view.annotation isKindOfClass:[MAUserLocation class]])
    {
        MAUserLocationRepresentation *pre = [[MAUserLocationRepresentation alloc] init];
        //自定义精度圈样式
//        pre.fillColor = [UIColor colorWithRed:0.9 green:0.1 blue:0.1 alpha:0.3];
//        pre.strokeColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.9 alpha:1.0];
//        pre.image = nil;
//        pre.lineWidth = 3;
//        pre.lineDashPattern = @[@6, @3];
        
        [self.mapView updateUserLocationRepresentation:pre];
        view.calloutOffset = CGPointMake(0, 0);
    } 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
